package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.UserSettingsDTO;
import com.zhiwen.service.UserSettingsService;
import com.zhiwen.domain.vo.UserSettingsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户设置控制器
 */
@Tag(name = "用户设置", description = "用户设置相关接口")
@RestController
@RequestMapping("/user-settings")
@RequiredArgsConstructor
public class UserSettingsController {

    private final UserSettingsService userSettingsService;

    /**
     * 获取当前登录用户设置
     */
    @Operation(summary = "获取当前用户设置", description = "获取当前登录用户的设置信息，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "获取成功",
                content = @Content(schema = @Schema(implementation = UserSettingsVO.class)))
    @GetMapping
    @SaCheckLogin
    public Result<UserSettingsVO> getUserSettings() {
        Long userId = StpUtil.getLoginIdAsLong();
        UserSettingsVO settings = userSettingsService.getUserSettings(userId);
        return Result.success(settings);
    }

    /**
     * 更新当前登录用户设置
     */
    @Operation(summary = "更新用户设置", description = "更新当前登录用户的设置信息，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "更新成功",
                content = @Content(schema = @Schema(implementation = UserSettingsVO.class)))
    @PutMapping
    @SaCheckLogin
    public Result<UserSettingsVO> updateUserSettings(@RequestBody UserSettingsDTO settingsDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        settingsDTO.setUserId(userId);
        UserSettingsVO settings = userSettingsService.updateUserSettings(settingsDTO);
        return Result.success(settings);
    }

    /**
     * 更新通知设置
     */
    @Operation(summary = "更新通知设置", description = "更新当前登录用户的通知设置，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "更新成功",
                content = @Content(schema = @Schema(implementation = UserSettingsVO.class)))
    @PutMapping("/notifications/{type}")
    @SaCheckLogin
    public Result<UserSettingsVO> updateNotificationSettings(
            @Parameter(description = "通知类型(answer/comment/accept/like)") @PathVariable String type,
            @Parameter(description = "是否启用") @RequestParam boolean enabled) {
        Long userId = StpUtil.getLoginIdAsLong();
        UserSettingsVO settings = userSettingsService.updateNotificationSettings(userId, type, enabled);
        return Result.success(settings);
    }

    /**
     * 更新隐私设置
     */
    @Operation(summary = "更新隐私设置", description = "更新当前登录用户的隐私设置，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "更新成功",
                content = @Content(schema = @Schema(implementation = UserSettingsVO.class)))
    @PutMapping("/privacy/{type}")
    @SaCheckLogin
    public Result<UserSettingsVO> updatePrivacySettings(
            @Parameter(description = "隐私类型(online_status/messages)") @PathVariable String type,
            @Parameter(description = "是否启用") @RequestParam boolean enabled) {
        Long userId = StpUtil.getLoginIdAsLong();
        UserSettingsVO settings = userSettingsService.updatePrivacySettings(userId, type, enabled);
        return Result.success(settings);
    }

    /**
     * 更新界面设置
     */
    @Operation(summary = "更新界面设置", description = "更新当前登录用户的界面设置，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "更新成功",
                content = @Content(schema = @Schema(implementation = UserSettingsVO.class)))
    @PutMapping("/interface")
    @SaCheckLogin
    public Result<UserSettingsVO> updateInterfaceSettings(
            @Parameter(description = "是否启用深色模式") @RequestParam(required = false) Boolean darkMode,
            @Parameter(description = "语言设置") @RequestParam(required = false) String language) {
        Long userId = StpUtil.getLoginIdAsLong();
        UserSettingsVO settings = userSettingsService.updateInterfaceSettings(userId, darkMode, language);
        return Result.success(settings);
    }

    /**
     * 重置用户设置
     */
    @Operation(summary = "重置用户设置", description = "将当前登录用户的设置重置为默认值，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "重置成功",
                content = @Content(schema = @Schema(implementation = UserSettingsVO.class)))
    @PostMapping("/reset")
    @SaCheckLogin
    public Result<UserSettingsVO> resetUserSettings() {
        Long userId = StpUtil.getLoginIdAsLong();
        UserSettingsVO settings = userSettingsService.resetUserSettings(userId);
        return Result.success(settings);
    }
}
